<?php
header('content-type:text/html; charset=utf-8');

$server = 'b-elyoym8jkhtkgj.bch.rds.gz.baidubce.com:3306';
$username = 'b_elyoym8jkhtkgj';
$password = 'mys123456';
$dbname = 'b_elyoym8jkhtkgj';
$conn = mysqli_connect($SERVER, $username, $password, $dbname);

if (!$conn) {
    die('数据库连接失败');
}
$obj = array(
    'code' => 0,
    'msg' => '登录失败',
  );
  
  if(!isset($_REQUEST['username']) || !isset($_REQUEST['password'])) {  // 判断参数是否传入
    $obj['msg'] = '缺少必要参数';
    die(json_encode($obj));
  }
  header("Access-Control-Allow-Origin:*");
  $username = $_REQUEST['username'];
  $password = $_REQUEST['password'];

  $sql = "SELECT * from myinfo WHERE username = '$username'";
  
  $result = mysqli_query($conn, $sql);
  
  $data = mysqli_fetch_assoc($result);
  
  if ($data) {
    //   判断从数据库里拿来的密码对比用户输入的密码是否相等
    if ($data['password'] === $password) {
      $obj['code'] = 1;
      $obj['msg'] = '登录成功';
    } else {
      $obj['msg'] = '用户名或者密码错误';
    }
  } else {
    $obj['msg'] = '用户名或者密码错误';
  }
  
  echo json_encode($obj);
